package fuse.LaCollaFS.Api;

import java.rmi.RemoteException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import LaColla.Api.ApplicationsSideApi;
import LaColla.core.data.Event;
import LaColla.core.data.GroupInfo;
import LaColla.core.data.TimestampSummary;

/**
 * Aquesta classe implementa l'API de la aplicaci� per que LaColla pugui fer-li crides
 */
public class ApiBase extends java.rmi.server.UnicastRemoteObject implements ApplicationsSideApi{


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TimestampSummary receivedEventsSummary=new TimestampSummary();
	private ApiImpl asai=new ApiImpl(); //application-depedent 
	
	public ApiBase() throws RemoteException {
		super();
	}

	//explicit destructor.
	public void close() {
		try{
			java.io.ObjectOutputStream oos = new ObjectOutputStream(
												new java.io.FileOutputStream(
													new java.io.File("lc_events.out")));
			oos.writeObject(receivedEventsSummary);
		}catch(Exception e){}
		
	}	
	
	public String getMemberId() {
		return asai.getMemberId();
	}

	public ApiImpl getASAI() {
		return asai;
	}

	public TimestampSummary readSummary() {
		try{
			java.io.ObjectInputStream ois = new ObjectInputStream(
												new java.io.FileInputStream(
													new java.io.File("lc_events.out")));
			receivedEventsSummary=(TimestampSummary)ois.readObject();
			
			}catch(Exception e){System.out.println(e);}
		return receivedEventsSummary;
	}
	
	public void initialize(Object o) {
		try {
		asai.initialize(o);
		}catch(Exception e){}
	}
	

	
	/*
	public void setAplicationId(String apId){
		this.aplicId=apId;
		asai.setAplicationId(apId);
	}
	*/

	public void exception(String groupId, String message) throws RemoteException {
		asai.exception(groupId,message);
	}

	/**
	 * LaColla crida aquest m�tode quan es rep un missatge instant�ni
	 */
	public void newInstantMsg(String group, String recipient, String sender, Object message) throws RemoteException {
		asai.newInstantMsg(group,recipient,sender,message);
	}

/**
 * LaColla ens notifica que s'ha connectat un nou usuari. 
 * Aquest event es rep nomes un cop, al fer el login.
 * 
 */
	public void newConnectedMember(String groupId,String userName, String mid) throws java.rmi.RemoteException{
		asai.newConnectedMember(groupId,userName,mid);
	}
	 
	/**
	 * LaColla ens informa que un usuari s'ha desconnectat
	 */
	public void memberDisconnected(String groupId,String userId) throws java.rmi.RemoteException {
		asai.memberDisconnected(groupId,userId);
	}
	
	/**
	 * LaColla ens informa d'un event a tot el grup
	 */
	public void newEvent(String groupId,Event evt)throws java.rmi.RemoteException{
	//	System.out.println("eoooo");
		receivedEventsSummary.update(evt.getTimestamp());
	//	System.out.println("eiiiii");
		asai.newEvent(groupId,evt);		
	}




	/* (non-Javadoc)
	 * @see LaColla.Api.ApplicationsSideApi#newInfoGroup(java.lang.String, java.lang.String, java.lang.String, LaColla.core.data.GroupInfo)
	 */
	public void newInfoGroup(String userId,	String groupId,	String aplicId,	GroupInfo info)	throws RemoteException {
		asai.newInfoGroup(userId,groupId,aplicId,info);
	}


	/* (non-Javadoc)
	 * @see LaColla.Api.ApplicationsSideApi#notifyStopTask(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void notifyStopTask(String groupId, String idTask, String result) throws RemoteException {
		asai.notifyStopTask(groupId,idTask,result);
	}

		
	/** 
	 * LaColla ens demana cada cert temps si l'aplicaci� est� viva. Sempre responem afirmativament per que no ens faci fora.
	 */	
	public boolean AppIsAlive(String appId) throws RemoteException {
		return asai.AppIsAlive(appId);
	}

		/* (non-Javadoc)
		 * @see LaColla.Api.ApplicationsSideApi#notifyTaskState(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
		 */
	public void notifyTaskState(String groupId, String idTask, String state, String result) throws RemoteException {
			asai.notifyTaskState(groupId,idTask,state,result);
	}
		
	
}
